<html>
  <head>
  <title>textDisplay.py</title>
  </head>
  <body>
  <h3>textDisplay.py (<a href="../textDisplay.py">original</a>)</h3>
  <hr>
  <pre>
<span style="color: green; font-style: italic"># textDisplay.py
# --------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html

</span><span style="color: blue; font-weight: bold">import </span>pacman<span style="font-weight: bold">, </span>time

DRAW_EVERY <span style="font-weight: bold">= </span><span style="color: red">1
</span>SLEEP_TIME <span style="font-weight: bold">= </span><span style="color: red">0 </span><span style="color: green; font-style: italic"># This can be overwritten by __init__
</span>DISPLAY_MOVES <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
</span>QUIET <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False </span><span style="color: green; font-style: italic"># Supresses output

</span><span style="color: blue; font-weight: bold">class </span>NullGraphics<span style="font-weight: bold">:
  </span><span style="color: blue; font-weight: bold">def </span>initialize<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state<span style="font-weight: bold">, </span>isBlue <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">pass
  
  def </span>update<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">pass
  
  def </span>pause<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span>time<span style="font-weight: bold">.</span>sleep<span style="font-weight: bold">(</span>SLEEP_TIME<span style="font-weight: bold">)
    
  </span><span style="color: blue; font-weight: bold">def </span>draw<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">print </span>state
  
  <span style="color: blue; font-weight: bold">def </span>finish<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">pass

class </span>PacmanGraphics<span style="font-weight: bold">:
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>speed<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span>speed <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">global </span>SLEEP_TIME
      SLEEP_TIME <span style="font-weight: bold">= </span>speed
  
  <span style="color: blue; font-weight: bold">def </span>initialize<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state<span style="font-weight: bold">, </span>isBlue <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>draw<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>pause<span style="font-weight: bold">()
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>turn <span style="font-weight: bold">= </span><span style="color: red">0
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>agentCounter <span style="font-weight: bold">= </span><span style="color: red">0
    
  </span><span style="color: blue; font-weight: bold">def </span>update<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state<span style="font-weight: bold">):
    </span>numAgents <span style="font-weight: bold">= </span>len<span style="font-weight: bold">(</span>state<span style="font-weight: bold">.</span>agentStates<span style="font-weight: bold">)
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>agentCounter <span style="font-weight: bold">= (</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>agentCounter <span style="font-weight: bold">+ </span><span style="color: red">1</span><span style="font-weight: bold">) % </span>numAgents
    <span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>agentCounter <span style="font-weight: bold">== </span><span style="color: red">0</span><span style="font-weight: bold">:
      </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>turn <span style="font-weight: bold">+= </span><span style="color: red">1
      </span><span style="color: blue; font-weight: bold">if </span>DISPLAY_MOVES<span style="font-weight: bold">:
        </span>ghosts <span style="font-weight: bold">= [</span>pacman<span style="font-weight: bold">.</span>nearestPoint<span style="font-weight: bold">(</span>state<span style="font-weight: bold">.</span>getGhostPosition<span style="font-weight: bold">(</span>i<span style="font-weight: bold">)) </span><span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">, </span>numAgents<span style="font-weight: bold">)]
        </span><span style="color: blue; font-weight: bold">print </span><span style="color: red">"%4d) P: %-8s" </span><span style="font-weight: bold">% (</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>turn<span style="font-weight: bold">, </span>str<span style="font-weight: bold">(</span>pacman<span style="font-weight: bold">.</span>nearestPoint<span style="font-weight: bold">(</span>state<span style="font-weight: bold">.</span>getPacmanPosition<span style="font-weight: bold">()))),</span><span style="color: red">'| Score: %-5d' </span><span style="font-weight: bold">% </span>state<span style="font-weight: bold">.</span>score<span style="font-weight: bold">,</span><span style="color: red">'| Ghosts:'</span><span style="font-weight: bold">, </span>ghosts
      <span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>turn <span style="font-weight: bold">% </span>DRAW_EVERY <span style="font-weight: bold">== </span><span style="color: red">0</span><span style="font-weight: bold">:
        </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>draw<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
        </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>pause<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">if </span>state<span style="font-weight: bold">.</span>_win <span style="color: blue; font-weight: bold">or </span>state<span style="font-weight: bold">.</span>_lose<span style="font-weight: bold">:
      </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>draw<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
    
  </span><span style="color: blue; font-weight: bold">def </span>pause<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span>time<span style="font-weight: bold">.</span>sleep<span style="font-weight: bold">(</span>SLEEP_TIME<span style="font-weight: bold">)
    
  </span><span style="color: blue; font-weight: bold">def </span>draw<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">print </span>state
  
  <span style="color: blue; font-weight: bold">def </span>finish<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">pass
</span>
  </pre>
  </body>
  </html>
  